<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="55%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :inline="true" :rules="formRules" :disabled="true"
        v-loading="formLoading" label-width="140px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="样本编码" prop="simpleCode">
              <el-input v-model="formData.simpleCode" class="form-input" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主体名称" prop="marketInfoName">
              <el-input v-model="formData.marketInfoName" class="form-input" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="采样时间" prop="sampleTakeDate">
              <el-date-picker clearable v-model="formData.sampleTakeDate" type="datetime" value-format="timestamp"
                class="form-date-picker" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="样本产地" prop="productOrigin">
              <el-input v-model="formData.productOrigin" class="form-input" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="具体送样人" prop="sendPersonName">
              <el-input v-model="formData.sendPersonName" class="form-input" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采样基数（千克）" prop="simpleTakeKg">
              <el-input v-model="formData.simpleTakeKg" class="form-input" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="采样地址" prop="simpleTakeAddress">
              <el-input v-model="formData.simpleTakeAddress" class="form-input" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="formData.remark" class="form-input" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="样本类型" prop="simpleTypeName">
              <el-input v-model="formData.simpleTypeName" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="样本子类" prop="simpleTowLevelName">
              <el-input v-model="formData.simpleTowLevelName" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="样品名称" prop="simpleName">
              <el-input v-model="formData.simpleName" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="现场照片" prop="simpleLivePicture">
              <el-image style="width: 100px; height: 100px" :preview-src-list="[formData.simpleLivePicture]"
                :src="formData.simpleLivePicture"></el-image>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="采样员签字照片" prop="signPicture">
              <el-image style="width: 100px; height: 100px" :preview-src-list="[formData.signPicture]"
                :src="formData.signPicture"></el-image>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button> -->
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as TakeDataApi from '@/api/market/takedata';
export default {
  name: "TakeDataForm",
  components: {
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        simpleCode: undefined,
        marketInfoName: undefined,
        simpleTypeId: undefined,
        simpleTypeName: undefined,
        simpleTowLevelId: undefined,
        simpleTowLevelName: undefined,
        simpleId: undefined,
        simpleName: undefined,
        sampleTakeDate: undefined,
        sampleCheckDate: undefined,
        productOrigin: undefined,
        sendPersonName: undefined,
        simpleTakeKg: undefined,
        simpleLivePicture: undefined,
        signPicture: undefined,
        simpleTakeAddress: undefined,
        remark: undefined,
        dataSourceId: undefined,
      },
      // 表单校验
      formRules: {
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await TakeDataApi.getTakeData(id);
          this.formData = res.data;
          this.title = "修改样品采样";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增样品采样";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await TakeDataApi.updateTakeData(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await TakeDataApi.createTakeData(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        simpleCode: undefined,
        marketInfoName: undefined,
        simpleTypeId: undefined,
        simpleTypeName: undefined,
        simpleTowLevelId: undefined,
        simpleTowLevelName: undefined,
        simpleId: undefined,
        simpleName: undefined,
        sampleTakeDate: undefined,
        sampleCheckDate: undefined,
        productOrigin: undefined,
        sendPersonName: undefined,
        simpleTakeKg: undefined,
        simpleLivePicture: undefined,
        signPicture: undefined,
        simpleTakeAddress: undefined,
        remark: undefined,
        dataSourceId: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
